/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.boxin.ims.modules.wechat.web;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.boxin.framework.base.utils.DateUtils;
import com.boxin.ims.modules.wechat.entity.MemberScore;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.service.MemberScoreService;
import com.boxin.ims.modules.wechat.service.WechatMemberService;
import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.modules.sys.entity.User;
import com.thinkgem.jeesite.modules.sys.utils.UserUtils;

/**
 * 会员积分Controller
 * @author Jakemanse
 * @version 2013-09-08
 */
@Controller
@RequestMapping(value = Global.ADMIN_PATH+"/wechat/memberScore")
public class MemberScoreController extends BaseController {

	@Autowired
	private MemberScoreService memberScoreService;
	
	@Autowired
	private WechatMemberService wechatMemberService;
	
	@ModelAttribute
	public MemberScore get(@RequestParam(required=false) Long id) {
		if (id != null){
			return memberScoreService.get(id);
		}else{
			return new MemberScore();
		}
	}
	
	@RequiresPermissions("wechat:memberScore:view")
	@RequestMapping(value = {"list", ""})
	public String list(MemberScore memberScore, HttpServletRequest request, HttpServletResponse response, Model model) {
		User user = UserUtils.getUser();
		if(!user.isAdmin()){
			memberScore.setUser(user);
		}
		
		if(memberScore.getStartTime() == null){
			Date now = DateUtils.getDayStartTime();
			memberScore.setCreateTime(now);
			memberScore.setStartTime(now);
		}else{
			memberScore.setCreateTime(memberScore.getStartTime());
		}
		
		
        Page<MemberScore> page = memberScoreService.find(new Page<MemberScore>(request, response), memberScore); 
        model.addAttribute("page", page);
        model.addAttribute("memberScore", memberScore);
		return "modules/wechat/memberScoreList";
	}

	@RequiresPermissions("wechat:memberScore:view")
	@RequestMapping(value = "form")
	public String form(MemberScore memberScore, Model model) {
		model.addAttribute("memberScore", memberScore);
		return "modules/wechat/memberScoreForm";
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-25  上午8:00:45
	 * @function <p> 充值积分 </p>
	 * @param memberScore
	 * @param request
	 * @param response
	 * @param model
	 * @return
	 */
	@RequiresPermissions("wechat:memberScore:view")
	@RequestMapping(value = "tocharge")
	public String chargeForm(MemberScore memberScore,HttpServletRequest request, HttpServletResponse response, Model model) {
		model.addAttribute("memberScore", memberScore);
		return "modules/wechat/memberScoreForm";
	}
	
	/**
	 * @author Jakemanse
	 * @time 2013-9-25  上午8:01:04
	 * @function <p>  </p>
	 * @param params
	 * @param model
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("wechat:memberScore:edit")
	@RequestMapping(value = "saveCharge")
	public String saveChargeScore(Map<String,String> params, Model model, RedirectAttributes redirectAttributes) {
		//memberScoreService.save(memberScore);
		addMessage(redirectAttributes, "保存会员积分成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/memberScore/?repage";
	}

	@RequiresPermissions("wechat:memberScore:edit")
	@RequestMapping(value = "save")
	public String save(MemberScore memberScore, Model model,HttpServletRequest request, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, memberScore)){
			return form(memberScore, model);
		}
		
		memberScoreService.save(memberScore);
		addMessage(redirectAttributes, "保存会员积分成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/memberScore/?repage";
	}
	
	@RequiresPermissions("wechat:memberScore:edit")
	@RequestMapping(value = "delete")
	public String delete(Long id, RedirectAttributes redirectAttributes) {
		memberScoreService.delete(id);
		addMessage(redirectAttributes, "删除会员积分成功");
		return "redirect:"+Global.ADMIN_PATH+"/wechat/memberScore/?repage";
	}

}
